/*
 * @Author: YuanQuan D
 * @Date: 2025-03-18 15:00:45
 * @LastEditTime: 2025-04-09 10:47:13
 * @FilePath: \F429_arm_control_slave\User_Code\Src\remote_control_decode.c
 */
#include "remote_control_decode.h"
#include "pc_interaction.h"
#include "pneumatic_muscle_control.h"

extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;
extern TIM_HandleTypeDef htim5;
extern TIM_HandleTypeDef htim12;

//对应遥控器123456通道 1 2默认已经电机使用
RC_channelTypedef RC_channel_1;
RC_channelTypedef RC_channel_2;
RC_channelTypedef RC_channel_3;
RC_channelTypedef RC_channel_4;
RC_channelTypedef RC_channel_5;
RC_channelTypedef RC_channel_6;
RC_channelTypedef RC_channel_7;
uint8_t remote_ctr_mode = 0;

void Channel_Capture_printf(void)
{
    printf("\r\n通道一捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_1.frequency,RC_channel_1.duty_cycle); // 7 10 14
    printf("\r\n通道二捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_2.frequency,RC_channel_2.duty_cycle);
    printf("\r\n通道三捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_3.frequency,RC_channel_3.duty_cycle);
    printf("\r\n通道四捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_4.frequency,RC_channel_4.duty_cycle);
    printf("\r\n通道五捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_5.frequency,RC_channel_5.duty_cycle);
    printf("\r\n通道六捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_6.frequency,RC_channel_6.duty_cycle);
}

//脉冲捕获引脚
//按通道排列
//PA0 PB4 PD12 PB8 PH10 PH12 PB14

//PA0         PB4       PD12       PB8       PH10      PH12       PB14
//TIM2_CH1  TIM3_CH1  TIM4_CH1  TIM4_CH3   TIM5_CH1  TIM5_CH3   TIM12_CH1


//左中右 对应 7.1 10.7 14.3
//右和上：14 ---线性通道1234
//通道5、6 三档位 上7  
//通道7   右 14.2 上 11.9 左 9.09 下 7.1
void Remote_Pwm_Capture_Init(void)
{
    //PWM输入捕获
    HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);
    HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);
    HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_2);
    HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_3);
    HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_4);
    HAL_TIM_IC_Start_IT(&htim5,TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&htim5,TIM_CHANNEL_2);
    HAL_TIM_IC_Start_IT(&htim5,TIM_CHANNEL_3);
    HAL_TIM_IC_Start_IT(&htim5,TIM_CHANNEL_4);
    HAL_TIM_IC_Start_IT(&htim12,TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&htim12,TIM_CHANNEL_2);
}

//通道1234
//1：前后  2：左右   ID1：170 前后 --- 270  ID2： 187 左右 --- 90
//3：腕部前后控制 4暂定空
//5：腕部前后或左右循环
//6：肩部二姿态循环
//7：肘部姿态调整
void Remote_map(void)
{
    RC_channel_1.real_Ctr_ang = map_float_to_int(RC_channel_1.duty_cycle, remote_duty_min, remote_duty_max, 90, 290);
    RC_channel_2.real_Ctr_ang = map_float_to_int(RC_channel_2.duty_cycle, remote_duty_min, remote_duty_max, 70, 270);
    RC_channel_3.real_Ctr_ang = map_float_to_int(RC_channel_3.duty_cycle, remote_duty_min, remote_duty_max, wrist_trachea_min_limit - 200, wrist_trachea_max_limit);
    RC_channel_7.real_Ctr_ang = map_float_to_int(RC_channel_7.duty_cycle, remote_duty_min, remote_duty_max, trachea_zero_output + 5, elbow_trachea_max_limit);
    if(RC_channel_5.duty_cycle >= remote_duty_max - 1)
    {
      remote_ctr_mode = 1;
    }
    else remote_ctr_mode = 0;
}

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	/*PWM 信号的第1个上升沿时，定时器产生中断，计数器，CCR寄存器被复位
    当下降沿到来时，IC2 会捕获，对应的是脉冲宽度测量，但不会产生中断。当
	  二个上升沿时，IC1会捕获，对应的是周期宽度测量*/
    if(htim == &htim2)
    {
      // RC_channel_1.CCR1 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_1);
      // if(RC_channel_1.CCR1 != 0)
      // {
      //   RC_channel_1.CCR2 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_2);
      //   RC_channel_1.frequency = (float)cnt_clk / (RC_channel_1.CCR1 + 1);
      //   RC_channel_1.duty_cycle = (float)(RC_channel_1.CCR2 + 1) * 100 / (RC_channel_1.CCR1 + 1);	
      //   RC_channel_1.end_flag = 1;
      // }
      // else
      // {
      //   RC_channel_1.frequency = 0;
      //   RC_channel_1.duty_cycle = 0;
      // }

      if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
      {
        RC_channel_1.CCR1 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_1);
      }
      else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
      {
        if(RC_channel_1.CCR1 != 0)
        {
          RC_channel_1.CCR2 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_2);
          RC_channel_1.frequency = (float)cnt_clk / (RC_channel_1.CCR1 + 1);
          RC_channel_1.duty_cycle = (float)(RC_channel_1.CCR2 + 1) * 100 / (RC_channel_1.CCR1 + 1);	
          RC_channel_1.end_flag = 1;
        }
      }
    }

    if(htim == &htim3)
    {
      RC_channel_2.CCR1 = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_1);
      if(RC_channel_2.CCR1 != 0)
      {
        RC_channel_2.CCR2 = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2);
        RC_channel_2.frequency = (float)cnt_clk / (RC_channel_2.CCR1 + 1);
        RC_channel_2.duty_cycle = (float)(RC_channel_2.CCR2 + 1) * 100 / (RC_channel_2.CCR1 + 1);	
        RC_channel_2.end_flag = 1;
      }
      else
      {
        RC_channel_2.frequency = 0;
        RC_channel_2.duty_cycle = 0;
      }
    }

    if(htim == &htim4)
    {
      RC_channel_4.CCR1 = HAL_TIM_ReadCapturedValue(&htim4, TIM_CHANNEL_1);
      if(RC_channel_4.CCR1 != 0)
      {
        RC_channel_4.CCR2 = HAL_TIM_ReadCapturedValue(&htim4, TIM_CHANNEL_2);
        RC_channel_4.frequency = (float)cnt_clk / (RC_channel_4.CCR1 + 1);
        RC_channel_4.duty_cycle = (float)(RC_channel_4.CCR2 + 1) * 100 / (RC_channel_4.CCR1 + 1);	
        RC_channel_4.end_flag = 1;
      }
      else
      {
        RC_channel_4.frequency = 0;
        RC_channel_4.duty_cycle = 0;
      }

      RC_channel_3.CCR1 = HAL_TIM_ReadCapturedValue(&htim4, TIM_CHANNEL_3);
      if(RC_channel_3.CCR1 != 0)
      {
        RC_channel_3.CCR2 = HAL_TIM_ReadCapturedValue(&htim4, TIM_CHANNEL_4);
        RC_channel_3.frequency = (float)cnt_clk / (RC_channel_3.CCR1 + 1);
        RC_channel_3.duty_cycle = (float)(RC_channel_3.CCR2 + 1) * 100 / (RC_channel_3.CCR1 + 1);	
        RC_channel_3.end_flag = 1;
      }
      else
      {
        RC_channel_3.frequency = 0;
        RC_channel_3.duty_cycle = 0;
      }
    }

    if(htim == &htim5)
    {
      RC_channel_6.CCR1 = HAL_TIM_ReadCapturedValue(&htim5, TIM_CHANNEL_1);
      if(RC_channel_6.CCR1 != 0)
      {
        RC_channel_6.CCR2 = HAL_TIM_ReadCapturedValue(&htim5, TIM_CHANNEL_2);
        RC_channel_6.frequency = (float)cnt_clk / (RC_channel_6.CCR1 + 1);
        RC_channel_6.duty_cycle = (float)(RC_channel_6.CCR2 + 1) * 100 / (RC_channel_6.CCR1 + 1);	
        RC_channel_6.end_flag = 1;
      }
      else
      {
        RC_channel_6.frequency = 0;
        RC_channel_6.duty_cycle = 0;
      }

      RC_channel_7.CCR1 = HAL_TIM_ReadCapturedValue(&htim5, TIM_CHANNEL_3);
      if(RC_channel_7.CCR1 != 0)
      {
        RC_channel_7.CCR2 = HAL_TIM_ReadCapturedValue(&htim5, TIM_CHANNEL_4);
        RC_channel_7.frequency = (float)cnt_clk / (RC_channel_7.CCR1 + 1);
        RC_channel_7.duty_cycle = (float)(RC_channel_7.CCR2 + 1) * 100 / (RC_channel_7.CCR1 + 1);	
        RC_channel_7.end_flag = 1;
      }
      else
      {
        RC_channel_7.frequency = 0;
        RC_channel_7.duty_cycle = 0;
      }
    }
   
    if(htim == &htim12)
    {
      RC_channel_5.CCR1 = HAL_TIM_ReadCapturedValue(&htim12, TIM_CHANNEL_1);
      if(RC_channel_5.CCR1 != 0)
      {
        RC_channel_5.CCR2 = HAL_TIM_ReadCapturedValue(&htim12, TIM_CHANNEL_2);
        RC_channel_5.frequency = (float)cnt_clk / (RC_channel_5.CCR1 + 1);
        RC_channel_5.duty_cycle = (float)(RC_channel_5.CCR2 + 1) * 100 / (RC_channel_5.CCR1 + 1);	
        RC_channel_5.end_flag = 1;
      }
      else
      {
        RC_channel_5.frequency = 0;
        RC_channel_5.duty_cycle = 0;
      }
    }
}